package pack;

import java.util.Date;

public class Interpreter {
    
    /*
     * Structure of an HTTP request:
     * 
     * COMMAND /ADDRESS HTTP-VERSION
     * Header: data
     * 
     * Example:
     * GET / HTTP/1.1
     */
    private static Network nt = new Network("aps/aps.tsv");
	private static Date today = new Date();
	private static String response_model = "HTTP/1.1 200 OK\n" +
			"Date: %s\n" +
			"Server: Absaber/0.1 (Unix) (Fedora/Linux)\n" +
			"Content-Type: text/html; charset=UTF-8\n" +
			"Content-Length: %d\n" +
			"Connection: close\n" +
			"\n" +
			"<html><head>\n" + 
			"<title>An Example Page</title>\n" +
			"</head><body>\n" +
			"Hello World, this is a very simple HTML document.\n" +
			"%s" + 
			"</body></html>";
	
    @SuppressWarnings("null")
	public static String understand(StringBuilder http_request){
        if(http_request != null && http_request.length() > 0){        	
        	String[] raw = http_request.toString().split(" ");
        	
        	int temp = 1;
        	if(raw[0].equals("GET"))
        		temp = 0;
        	
        	
        	switch(temp){
        		case 0: return doGet(http_request);
        		
        		default: return "HTTP/1.1 200 OK" +
								"Date: "+ today.toString() + "\n" +
								"Server: Absaber/0.1 (Unix) (Fedora/Linux)" +
								"Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT" +
								"Content-Type: text/html; charset=UTF-8" +
								"Content-Length: 131" +
								"Connection: close" +
								"\n\n" +
								"<html><head>" + 
								"<title>An Example Page</title>" +
								"</head><body>" +
								"Hello World, this is a very simple HTML document." +
								"</body></html>";
        	}
        }
        return "";
    }
    
    public static String doGet(StringBuilder request){

    	String mac = request.toString().split(" ")[1].replace("/", "");
    	System.out.println(mac);
    	String response = String.format(response_model, today.toString(), 
    			today.toString().length() + response_model.length() + 200,
    			"AP: " + Network.search(mac) );
    	return response;
    }
}